<?php $this->Html->script('jquery-1.7.2.min',false);?>
<?php $this->Html->script('jquery-ui-1.8.22.custom.min',false);?>
<?php echo $this->Html->css('jquery-ui-1.8.22.custom.css'); ?>
<script type="text/javascript">
	var itemCont = <?php 
	$desgloce = explode(";",$this->Form->value('Service.desgloce'),-1);
	$numItems = count($desgloce);
	echo $numItems < 2 ? '2' : ($numItems/2)+1;?>;

	

	
	function addFormFields()
	{
		var concept = '<tr><td><div class="input text"><input required="1" type="text" id="ServiceConcept" name="data[Item]['+itemCont+'][concept]"></div></td>';
		var price = '<td><div class="input text"><input required="1" type="text" id="ServicePrice" name="data[Item]['+itemCont+'][price]"></div></td>';
		var button = '<td><button type="button" onclick="javascript:delFormFields(\'data[Item]['+itemCont+'][price]\');">X</button></td></tr>'
		$('div table tr:last-child').after(concept+price+button);
		$('input[name="data[Item]['+itemCont+'][concept]"]').focus()
		itemCont++;
	}

	function delFormFields(name)
	{
		$('input[name="'+name+'"]').parent().parent().parent().remove();
	}
	
</script>
<div class="services form">
<?php echo $this->Form->create('Service'); ?>
	<fieldset>
		<legend><?php echo __('Editar Servicio'); ?></legend>
	<?php
		echo $this->Form->input('id');
		echo $this->Form->hidden('car_id');
		echo $this->Form->input('numservicio',array('readonly'=>true));
		echo $this->Form->input('descripcion',array('label'=>'trabajo'));
		echo $this->Form->input('fecha',array('type'=>'text', 'id'=>'datePicker','readonly'=>false));
		echo $this->Form->input('kilometraje',array('required'=>true));
		
		if($numItems > 0){
			//debug($numItems);
			//debug($desgloce);
			echo '<table><th>Concepto</th><th>Monto</th>';
			for ($i = 0; $i < $numItems; $i+=2) {
				echo '<tr><td>';
				echo $this->Form->input('concept',array('label'=> false,'value'=>$desgloce[$i],'name'=>'data[Item]['.$i.'][concept]','required'=>true));
				echo '</td><td>';
				echo $this->Form->input('price',array('label'=>false,'value'=>$desgloce[$i+1],'name'=>'data[Item]['.$i.'][price]','required'=>true));
				//echo '</td><td>'.'</td></tr>';
				echo '</td><td><button class="btnClose" type="button" onclick="javascript:delFormFields(\'data[Item]['.$i.'][price]\');">X</button></td></tr>';
			}
			echo '</table>';

		} else {
			echo '<table><th>Concepto</th><th>Monto</th>';
			echo '<tr><td>';
			echo $this->Form->input('item',array('label'=>false,'name'=>'data[Item][0][price]'));
			echo '</td><td>';
			echo $this->Form->input('price',array('label'=>false,'name'=>'data[Item][0][price]'));
			echo '</td><td><button class="btnClose" type="button" onclick="javascript:delFormFields(\'data[Item][0][price]\');">X</button>';
			echo '</td></tr></table>';
		}
		

	?>
	<button type="button" onclick="javascript:addFormFields();">Agregar descripcion</button>
	</fieldset>
<?php echo $this->Form->end(__('Editar')); ?>
</div>
<script>
	//$("#datePicker" ).datepicker({dateFormat:"dd-mm-yy"});

	$( "#datePicker" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat:"dd-mm-yy",
		showButtonPanel: true
	});

</script>
<div class="actions">
	<h3><?php echo __('Acciones'); ?></h3>
	<ul>

		<li><?php echo $this->Form->postLink(__('Borrar Servicio'), array('action' => 'delete', $this->Form->value('Service.id')), null, __('Esta seguro que desea borrar el servicio %s?', $this->Form->value('Service.numservicio'))); ?></li>
		<li><?php  echo $this->Html->link('Volver',"javascript:history.back()"); ?> </li>
	</ul>
</div>
